from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
import pymysql
from flask import Flask,render_template
from flask_migrate import Migrate,MigrateCommand
from flask_mongoengine import MongoEngine
import pymongo
from mongoengine import *



app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:zxf1986@localhost:3306/zsr_db'
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///zsr_db.db'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
db = SQLAlchemy(app)

app.config['MONGODB_SETTINGS'] = {'db':'zsr','host':'localhost','port': 27017,'username':'figo','password':'zxf1986'}
app.config['MONGO_DBNAME'] = 'zsr'
app.config['MONGO_URI'] = 'mongodb://figo:zxf1986@localhost:27017/zsr'
app.url_map.strict_slashes = False
mgdb = MongoEngine(app)



manager = Manager(app)

migrate=Migrate(app,db)
manager.add_command('db',MigrateCommand)


class User(db.Model):
    __tablename__='users'
    user_id=db.Column(db.Integer(),primary_key=True,autoincrement=True)
    user_name=db.Column(db.String(49))
    user_status=db.Column(db.Integer())
    user_born_time=db.Column(db.DateTime)
    user_lastlogin_time=db.Column(db.DateTime)

    #user_header_image=

    def __repr__(self):
        return '<User %r>' % self.user_name




class Wenji(db.Model):
    __tablename__='wenjis'
    wenji_id=db.Column(db.Integer(),primary_key=True,autoincrement=True)
    wenji_name=db.Column(db.String(49))
    wenji_status=db.Column(db.Integer())
    wenji_effective_time=db.Column(db.DateTime)
    wenji_disabled_time=db.Column(db.DateTime)
    creater_id=db.Column(db.Integer())
    wenduans=db.relationship('Wenduan',backref='wenji')

    def __repr__(self):
        return '<Wenji %r>' % self.wenji_name


class Wenduan(db.Model):
    __tablename__='wenduans'
    wenduan_id=db.Column(db.Integer(),primary_key=True,autoincrement=True)
    wenduan_name=db.Column(db.String(49))
    wenduan_status=db.Column(db.Integer())
    wenduan_uploaded_time=db.Column(db.DateTime)
    wenduan_wenji_id=db.Column(db.Integer,db.ForeignKey('wenjis.wenji_id'))

    def __repr__(self):
        return '<Wenduan %r>' % self.wenduan_name

class Test(mgdb.Document):
    # 字段
    name = mgdb.StringField()
    age = mgdb.StringField()
    def __str__(self):
        return "------name:{} ------- age:{}".format(self.name,self.age)


@app.route('/')
def hello_world():
    wenji=Wenji.query.first()
    todos = Test.objects().first()
    return render_template('index1.html',mgid=todos.name,wenjiname=wenji.wenji_name)

@app.route('/wenji/<wenji_id>')
def wenji_page(wenji_id):
    return 'wenji_id-> %s' % wenji_id


@app.route('/index')
def index():
    todos = Test.objects().first()
    return todos.name


if __name__ == '__main__':
   # app.run(host='0.0.0.0')
   app.debug = True
   manager.run()


